package com.sf.sms.examination.service.imp;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sf.sms.examination.service.SoftwareFlowService;
import com.sf.sms.software.dao.SoftwareMapper;
import com.sf.sms.software.entity.Software;
import com.sf.sms.software.entity.dto.SoftwareCategoryDTO;
import com.sf.sms.software.service.SoftwareCategoryService;
import com.sf.sms.utils.PropertyUtil;

@Service
public class SoftwareFlowServiceImp implements SoftwareFlowService {
	@Autowired
	SoftwareMapper softwareMapper;
	@Autowired
	private SoftwareCategoryService scService;

	@Override
	public Map<String, Object> flowPass(Software softwareEN, String path) {
		// 获取软件属性
		Software software = softwareMapper.getSoftwareById(softwareEN.getId());
		String newPath ="\\download"+software.getRemoteDownloadUrl().substring(software.getRemoteDownloadUrl().lastIndexOf("\\"));
		String	oldUrl = path+software.getRemoteDownloadUrl();
		String	newUrl = path+newPath;
		softwareEN.setStatus("2");// 通过审批
		//String imgDomainDir = PropertyUtil.getValue("img_domain_dir");//
		// 把文件放到指定位置
		
		softwareEN.setDownloadUrl(newPath.replaceAll("\\\\", "/"));
		softwareEN.setRemoteDownloadUrl(newPath);
		Object value = "success";
		boolean flag = coplyFile(oldUrl, newUrl);
		if (flag) {
			softwareMapper.updateSoftwareById(softwareEN);
		}else{
			
			value="error";
		}
		Map<String, Object> map = new HashMap<>();
		
		map.put("status", value);
		map.put("result", flag);
		return map;
	}

	
	
	private boolean coplyFile(String oldPath, String newPath) {
		// 声明读入，读出流
		InputStream inStream = null;
		FileOutputStream fs = null;
		try {
			int bytesum = 0;
			int byteread = 0;
			File oldfile = new File(oldPath);
			if (oldfile.exists()) { // 文件存在时
				inStream = new FileInputStream(oldPath); // 读入原文件
				fs = new FileOutputStream(newPath);
				byte[] buffer = new byte[1444];
				while ((byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread; // 字节数 文件大小
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
				fs.close();
			}
		} catch (Exception e) {
			System.out.println("复制单个文件操作出错");
			e.printStackTrace();
			return false;
		} finally {
			if ( inStream != null ) {
				try {
					inStream.close();
				} catch (IOException e) {
					e.printStackTrace();
					return false;
				}
			}
			if ( fs != null ) {
				try {
					fs.close();
				} catch (IOException e) {
					e.printStackTrace();
					return false;
				}
			}
		}
		return true;
	}

	@Override
	public Map<String, Object> flowRefuse(String softId) {
		Software software = softwareMapper.getSoftwareById(Long.parseLong(softId));
		software.setId(Long.parseLong(softId));
		software.setStatus("3");
		int result = softwareMapper.updateSoftwareById(software);
		boolean flag = false;
		if (result > 0) {
			flag = true;
		}
		Map<String, Object> map = new HashMap<>();
		Object value = "success";
		map.put("status", value);
		map.put("result", result);
		return map;
	}

	@Override
	public Map<String, Object> getSoftwareById(String softId) {
		Software software = softwareMapper.getSoftwareById(Long.parseLong(softId));
		
		List<SoftwareCategoryDTO> categoryDTO = scService.getAllSoftwareCategory();
		Map<String, Object>  map= new HashMap<>();
		Object value = "success";
		map.put("status", value);
		map.put("result", software);
		map.put("category", categoryDTO);
		return map;
	}

}
